import 'dart:convert';
import 'package:my_jingdongshop/services/Storage.dart';

class CheckOutServices {
  // 计算购物车中被选中商品总价
  static getAllPrice(checkOutListData) {
    var tempAllPrice = 0.0;
    for (var i = 0; i < checkOutListData.length; i++) {
      if (checkOutListData[i]["checked"] == true) {
        tempAllPrice +=
            checkOutListData[i]["price"] * checkOutListData[i]["count"];
      }
    }
    return tempAllPrice;
  }

  // 将购物车中被选中商品删除
  static removeUnSelectedCartItem() async {
    List cartList = [];
    List tempList = [];

    //获取购物车的数据
    try {
      cartList = json.decode(await Storage.getString('cartList') as String);
    } catch (e) {
      cartList = [];
    }

    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["checked"] == false) {
        tempList.add(cartList[i]);
      }
    }

    Storage.setString("cartList", json.encode(tempList));
  }
}
